<?php
namespace User\Controller;
use Wap\Model\EnrollModel;
use User\Controller\UserController;
use Think\Page;

class  EnrollController extends UserController
{
    public function __construct()
    {
        parent::__construct();
    }


    public function index()
    {
        $enroll = M('enroll');

        $count = $enroll->where(array('token' => $this->wxuser['token']))->count();

        $page = new Page($count, 15);
        $show = $page->show();

        $list = $enroll->where(array('token' => $this->wxuser['token']))->limit($page->firstRow . ',' . $page->listRows)->select();

        $this->assign('page', $show);
        $this->assign('list', $list);
        $this->display();

    }

    public function edit()
    {
        $condition['id'] = I("get.enroll", -1, "");
        $condition['token'] = $this->wxuser['token'];
        $enroll = D("enroll")->where($condition)->find();
        $this->assign("enroll", $enroll);
        $this->display();
    }

    public function data()
    {
        $condition['eid'] = I("get.enroll", -1, "");
        $condition['token'] = $this->wxuser['token'];
        $telephone = I("post.telephone", "", "");

        if ($telephone) {
            $condition['telephone'] = array("LIKE", "%$telephone%");
        }

        $mode = D("enrollRecord");
        $count = $mode->where($condition)->count();
        $page = new Page($count, 15);
        $show = $page->show();

        $enrollRecords = $mode->where($condition)->limit($page->firstRow . ',' . $page->listRows)->select();
        $enroll = D("enroll")->where(array("id" => $condition['eid'], "token" => $this->wxuser['token']))->find();

        $this->assign('page', $show);
        $this->assign("enroll", $enroll);
        $this->assign("telephone", $telephone);
        $this->assign("enrollRecords", $enrollRecords);
        $this->display("EnrollRecord/index");
    }

    public function update()
    {
        $id = I("post.id", -1, "");
        $data = array("title" => "", "info" => "", "picurl" => "", "keyword" => "", "end_time" => "", "begin_time" => "");
        $data = array_intersect_key($_POST, $data);

        $data['begin_time'] = strtotime($data['begin_time']);
        $data['end_time'] = strtotime($data['end_time']);

        $img = $this->upload();
        if ($img) {
            $data['picurl'] = $img;
        }
        $data['headimg'] = $data['picurl'];

        $mode = D("Enroll");
        $result = null;
        $mode->startTrans();
        if ($id != -1 && !empty($id)) {
            $data['id'] = $id;
            $result['id'] = $id;
            $mode->create($data);
            $result['updated'] = $mode->save($data);
            $result['success'] = $result['updated'];
        } else {
            $data['token'] = $this->wxuser['token'];
            $mode->create($data);
            $data['id'] = $mode->add($data);
            $result['id'] = $data['id'];
            $result['added'] = $data['id'] ? true : false;
            $result['success'] = $result['added'];
        }
        if ($result['success']) {
            $deleted = D("keyword")->where(array("token" => $this->wxuser['token'], 'pid' => $data['id']))->delete();
            if (!!$data['keyword']) {
                $keywords = explode(",", $data['keyword']);
                if ($deleted !== false) {
                    foreach ($keywords as $keyword) {//TODO: 批量插入
                        $added = D("keyword")->add(array("keyword" => $keyword, "pid" => $result['id'], "token" => $this->wxuser['token'], 'module' => "Enroll"));
                        if (!$added) {
                            $mode->rollback();
                            $result['success'] = false;
                        }
                    }
                } else if ($deleted === false) {
                    $mode->rollback();
                    $result['success'] = false;
                }
            }
        }
        $mode->commit();

        if ($result['success'] > 0 || ($result['success'] === 0 && $data['id'] > 0)) {
            header("Content-type: text/html; charset=utf-8");
            $this->redirect("User/Enroll/edit", array("enroll" => $data['id']), 5, "保存成功,正在返回。。。");
        } else {
            $this->error("保存失败", 5);
        }
    }

    public function delete()
    {
        $condition['id'] = I("get.enroll", -1, "");
        $condition['token'] = $this->wxuser['token'];
        $deleted = D("enroll")->where($condition)->delete();
        if ($deleted) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    private function upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/'; // 设置附件上传根目录
        $upload->savePath = "./Uploads/enroll/"; // 设置附件上传（子）目录
        $upload->subName = array("date", "Ymd");
        $upload->saveName = array("uniqid", "painting");
        // 上传文件
        $info = $upload->upload();
        $img = "";
        //if(!$info) {// 上传错误提示错误信息
        //$this->error($upload->getError());
        //}else{// 上传成功
        if ($info) {
            //$file =
            //$this->ajaxReturn( $info );
            //$temq	=	explode( "./", $file['savepath'], 2 );
            foreach ($info as $file) {
                $temq = explode("./", $file['savepath'], 2);
                $img = 'Public/' . $temq[1] . $file['savename'];
            }
        } else {
            //$this->ajaxReturn( $upload->getError() );
        }
        //$this->success('上传成功！');
        //}
        return $img;
    }

    public function test()
    {
        return $this->display();
    }
}

?>
